<template>
    <d2-container>
        <el-form :inline="true" :model="dataSearch" ref="dataSearch">
            <el-form-item label="班级名称" prop="name">
                <el-input v-model="dataSearch.name" placeholder="请输入班级名称"></el-input>
            </el-form-item>
             <el-form-item label="校区" prop="school" v-if='showSchool'>
                <el-select
                        v-model="dataSearch.school"
                        filterable
                        placeholder="请选择校区"
                        @change="schoolSelected"
                        :disabled="disabled"
                >
                    <el-option
                            v-for="item in schoolList"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id"
                    ></el-option>
                </el-select>
            </el-form-item>
      <el-form-item>
            <el-radio-group v-model="dataSearch.status" @change='changeStatus'>
          <el-radio :label="-2">全部</el-radio>
          <el-radio :label="-1">待进行</el-radio>
          <el-radio :label="0">进行中</el-radio>
          <el-radio :label="1">已结束</el-radio>
        </el-radio-group>
    </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="formSubmit">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="formReset">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item>
            <a href="#/class-new">
                <el-button type="primary" style="float:right" icon="el-icon-plus">新建班级</el-button>
            </a>
        </el-form>
        <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="name" label="班级名称"></el-table-column>
             <el-table-column label="授课老师" prop="teacher">
                <template slot-scope="scope">
                    {{scope.row.teacher == undefined ? '暂无' : scope.row.teacher.name}}
                    <!-- <el-button @click="handleClick(scope.row)" type="text" size="small">
                    </el-button> -->
                </template>
            </el-table-column>
            
            <el-table-column label="教务老师" prop="registrar">
                <template slot-scope="scope">
                    {{scope.row.registrar == undefined ? '暂无' : scope.row.registrar.name}}
                    <!-- <el-button @click="handleClick(scope.row)" type="text" size="small">
                    </el-button> -->
                </template>
            </el-table-column>

           

            <el-table-column prop="classroom" label="教室">
                 <template slot-scope="scope">
                    {{scope.row.classroom == '' ? '暂无' : scope.row.classroom}}
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column prop="course" label="课程"></el-table-column>
            <el-table-column prop="school" label="所在学校"></el-table-column>
            <el-table-column prop="studentLimit" label="班级人数">
                <template slot-scope="scope">
                    {{scope.row.studentAmount + ' / ' + scope.row.studentLimit}}
                </template>
            </el-table-column>
            <el-table-column  label="上课时间">
                 <template slot-scope="scope">
                <span style="white-space: pre-line;">{{scope.row.classHours}}</span>
                 </template >
            </el-table-column>
            <el-table-column  label="课程日期">
                 <template slot-scope="scope">
                <span style="white-space: pre-line;">{{scope.row.courseTime}}</span>
                 </template >
            </el-table-column>

            <el-table-column  label="操作">
                <template slot-scope="scope">
                    <el-button @click="handleClick2(scope.row)" type="text" size="normal" style='text-align: center;display: inline-block;'>查看
                    </el-button>
                    <el-button @click="changeClick(scope.row)" type="text" size="normal" style='text-align: center' v-if="!teacher">修改
                    </el-button>
                    <el-button @click="deleteClick(scope.row)" type="text" size="normal" style='text-align: center'>删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";

    export default {
        data() {
            return {
                dataList: [],
                total: 0,
                pageSize: 10,
                currentPage: 1,
                classroom: [],
                teacher : false,
                dataSearch: {status:-2,school:undefined},
                schoolLimit:undefined,
                schoolList : [],
                headquarter : 0,
                showSchool :false,
                cnweek : ['日','一','二','三','四','五','六']
            };
        },
        computed: {
            ...mapState('d2admin/user', ['info']),
        },

        methods: {
            formSubmit() {
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            formReset() {
                this.$refs.dataSearch.resetFields();
                this.getDataList(this.dataSearch)
            },
            handleClick(e) {
                this.$router.push({path: "/staff_new?id=" + e._id});
            },
            handleClick2(e) {

                this.$router.push({path: "/class-detail?class_id=" + e._id});
            },
            changeClick(e) {
                let path = "/class-change?id=" + e._id + '&school=' + this.dataSearch.school
                this.$router.push({path: path});
            },
            handleCurrentChange(currentPage) {
                this.currentPage = currentPage;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlerSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handleNextPage() {
                this.currentPage = this.currentPage + 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlePrevClick() {
                this.currentPage = this.currentPage - 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            deleteClick(e) {
                this.$confirm("此操作将删除该班级, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    request({
                        url: "/class/deleteClass",
                        method: "post",
                        data: {id: e._id}
                    }).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: "删除成功"
                            });
                            this.getDataList(this.dataSearch);
                        }
                    });
                }).catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },

            changeStatus (){
                this.getDataList(this.dataSearch)
            },
            getDataList(data) {

                data.schoolLimit = this.school
                let index1 = this.rights.indexOf('root') != -1
                let index2 = this.rights.indexOf('授课老师') != -1
                let index4 = this.rights.indexOf('教务老师') != -1
                let index3 = this.rights.indexOf('教务中心') != -1
                let index5 = this.rights.indexOf('校长') != -1
                index1 = index1 || index5
                index2 = index2 || index4
                if (this.headquarter){
                    data.schoolLimit = this.dataSearch.school
                }

                if ((!index1) && (!index3)){
                    if (index2){
                        data.teacher = this.staff == null ? 'unbind' : this.staff
                        delete data.schoolLimit
                    }
                }

                // data.school = this.school
                request({
                    url: "/class/getClassList",
                    method: "post",
                    data
                }).then(res => {
                    this.dataList = res.data.class;
                    this.total = res.data.total;
                    this.currentPage = res.data.currentPage;
                    this.dataList.forEach((item, index) => {
                        // this.dataList[index].registrar = item.registrar.name
                        // this.dataList[index].teacher = item.teacher.name
                        this.dataList[index].course = item.course == undefined ? '' : (item.course.name + '(' + item.course.duration + '小时)')
                        this.dataList[index].classroom = item.classroom == undefined ? '' : item.classroom.name 
                        if (this.dataList[index].school) {
                            this.dataList[index].school = item.school.name;
                        }
                        this.dataList[index].classHours = ''
                        let temp = ''

                        this.dataList[index].hours.forEach((hourx,index2)=>{
                            if (temp.length != 0){
                                // temp += '\n'
                            }
                            for (var i = 0 ; i < hourx.hours.length ;i++){
                                temp += '周' + this.cnweek[index2] + ':' + this.hourConvert(hourx.hours[i].start) + ' - ' + this.hourConvert(hourx.hours[i].end)
                            }
                        })
                        console.log(temp,'dsds',item.hours)
                        this.dataList[index].classHours = temp
                    });

                });
            },
                        getSchoolList() {
                let data = {};
                data.page = 1
                data.pageSize = 999999
                request({
                    url: "/school/getSchoolList",
                    method: "post",
                    data
                }).then(res => {
                    this.schoolList = res.data.school;
                    // 返回数据
                });
            },
            hourConvert(hour){
            let m = this.addZero(parseInt(hour * 30 % 60))
            let h = this.addZero(parseInt(Math.floor(hour /2 )))
            return String(h + ':' + m)
        },
        addZero(m){
            if (m < 10){
                return '0' + m
            }
            else{
                return String(m)
            }
        },

            load(){
                 this.school = this.info.school[0]._id
            this.staff = this.info.staff == undefined ? null : this.info.staff._id
            this.staffname = this.info.staff == undefined ? '' : this.info.staff.name
            this.rights = this.info.rights.map(function(e){return e.name})

            this.dataSearch.school = this.school

            this.dataSearch.page = this.currentPage;
            this.dataSearch.pageSize = this.pageSize;
            this.getDataList(this.dataSearch);

            let index1 = this.rights.indexOf('root') == -1
            let index2 = this.rights.indexOf('授课老师') != -1
            let index4 = this.rights.indexOf('授课老师') != -1
            index2 = (index2 || index4)
            let index3 = this.rights.indexOf('教务中心') == -1
            let index5 = this.rights.indexOf('校长') == -1
            let index = (index1 && index5)
            
            if (!index){
                this.headquarter = 1
                this.getSchoolList()
                this.showSchool = true
                this.dataSearch.school = this.info.school[0]._id
            }
            if ((index1) && (index2)){
                if (index3){
                    this.teacher = true
                    this.showSchool = false
                }
            }
            }
        },
        beforeRouteEnter(to,from,next){
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }

        },
        
        created() {

           this.load()
            
        },

        
    };
</script>
